package net.flyinggroup.client.service;

import net.flyinggroup.client.model.Country;
import org.springframework.graphql.client.HttpGraphQlClient;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

import java.util.List;

@Service
public class CountryClient {
    private final HttpGraphQlClient graphQlClient;

    public CountryClient() {
        WebClient client = WebClient.builder()
                .baseUrl("https://countries.trevorblades.com/")
                .build();
        graphQlClient = HttpGraphQlClient.builder(client).build();
    }

    public Mono<List<Country>> getCountries() {
        // language=GraphQL
        String document = """
                query {
                  countries {
                    name
                    emoji
                    currency
                    code
                    capital
                  }
                }
                """;
        return graphQlClient.document(document)
                .retrieve("countries")
                .toEntityList(Country.class);
    }
}
